package com.zmn.mcc.business.impl.dpermit.channel;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.mcc.business.interfaces.dpermit.channel.DpermitChannelBService;
import com.zmn.mcc.common.constant.DpermitConsts;
import com.zmn.mcc.manager.utils.DpermitUtil;
import com.zmn.mcc.model.dto.dpermit.DpermitBaseDO;
import com.zmn.mcc.model.dto.dpermit.DpermitCountDO;
import com.zmn.mcc.model.dto.dpermit.DpermitSimpleDO;
import com.zmn.mcc.model.entity.dept.McDeptDpermit;
import com.zmn.mcc.model.entity.organize.McOrganize;
import com.zmn.mcc.services.interfaces.organize.OrganizeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author heguanghua
 * @since 2020/05/26 16:34
 */
@Service
public class DpermitChannelBServiceImpl implements DpermitChannelBService {
    protected Logger logger = LoggerFactory.getLogger(DpermitChannelBServiceImpl.class);

    @Resource
    private OrganizeService organizeService;


    // region 取得值的一级渠道及其下级渠道的列表

    @Override
    public List<McDeptDpermit> listDpermitForSelfAndSub(List<Integer> oneChannelIds) {

        return null;
    }
    // endregion

    // region 取得全部下单平台及其一级渠道列表

    @Override
    @ReadOnlyConnection
    public List<DpermitSimpleDO> listChannelAndDpermitChecked(Integer status, List<? extends DpermitBaseDO> dpermitChannels) {

        List<DpermitSimpleDO> items = new ArrayList<>();

        // 平台ID和简单组织对象的映射关系
        Map<Integer, DpermitSimpleDO> map = new HashMap<>();
        Map<Integer, DpermitBaseDO> dpertmitMap = new LinkedHashMap<>();
        for (DpermitBaseDO dpermit : dpermitChannels) {
            dpertmitMap.put(dpermit.getDataId(), dpermit);
        }

        // 下单平台列表
        List<McOrganize> organizes = organizeService.listAboveByLevel(DpermitConsts.ORGANIZE_LEVEL_PLAT, status);
        for (McOrganize organize : organizes) {

            // 简单组织对象
            DpermitSimpleDO simple = this.parseToDpermitSimpleDO(organize);

            // 平台加入映射关系map
            map.put(simple.getDataId(), simple);

            // 选中已分配
            if (CollectionUtil.isNotNullOrEmpty(dpertmitMap) && dpertmitMap.containsKey(simple.getDataId())) {
                simple.setChecked(true);
            }

            // 平台加入主列表
            items.add(simple);
        }

        /**
         * 一级渠道加入下单平台的子集
         * 都是已分配的
         */
        for (DpermitBaseDO dpermit : dpertmitMap.values()) {
            if (dpermit.getDataLevel() != DpermitConsts.COOPERATION_LEVEL_COMPANY) {
                continue;
            }
            DpermitSimpleDO simple = map.get(dpermit.getDataParentId());    // DataParentId为plat
            if (simple == null) {
                continue;
            }
            simple.getSubors().add(DpermitUtil.parseToDpermitSimpleDO(dpermit));
        }

        return items;
    }
    // endregion

    // region 取得全部下单平台列表

    @Override
    @ReadOnlyConnection
    public List<DpermitSimpleDO> listChannelPlatAndDpermitChecked(Integer status, List<Integer> dpermitIds, List<DpermitCountDO> dpermitCounts) {

        List<DpermitSimpleDO> items = new ArrayList<>();

        // 组织ID和简单组织对象的映射关系
        Map<Integer, DpermitSimpleDO> map = new HashMap<>();
        Map<Integer, Integer> countMap = new HashMap<>();
        for (DpermitCountDO dpermitCount : dpermitCounts) {
            countMap.put(dpermitCount.getDataId(), dpermitCount.getCount());
        }

        // 下单平台列表
        List<McOrganize> organizes = organizeService.listAboveByLevel(DpermitConsts.ORGANIZE_LEVEL_PLAT, status);
        for (McOrganize organize : organizes) {

            // 简单组织对象
            DpermitSimpleDO simple = this.parseToDpermitSimpleDO(organize);

            // 平台加入映射关系map
            map.put(simple.getDataId(), simple);

            // 选中已分配
            if (CollectionUtil.isNotNullOrEmpty(dpermitIds) && dpermitIds.contains(simple.getDataId())) {
                simple.setChecked(true);
            }

            // 平台加入主列表
            items.add(simple);
        }

        /**
         * 特殊处理平台下的一级渠道全选状态
         * 平台选中且没有一级渠道则视为全选
         */
        for (DpermitSimpleDO simple : map.values()) {
            int count = Optional.ofNullable(countMap.get(simple.getDataId())).orElse(0);
            if (count > 0) {
                simple.setChecked(true);
                simple.setLowerSubCount(count);
                simple.setSubAll(false);
            }
        }

        /**
         * 检查并处理简单数据对象选中状态
         * 下级部分选中则上级选中，下级全部选中则对上级标识
         */
        DpermitUtil.checkSelected(items, false);

        return items;
    }
    // endregion

    /**
     * 转换为简单数据权限对象
     *
     * @param organize
     * @return
     */
    private DpermitSimpleDO parseToDpermitSimpleDO(McOrganize organize) {
        if (organize == null) {
            return null;
        }
        DpermitSimpleDO simple = new DpermitSimpleDO();
        simple.setDataId(organize.getPlat());
        simple.setDataName(organize.getOrgName());
        simple.setParentId(GlobalConsts.TOP_ID);
        simple.setStatus(organize.getStatus());
        simple.setLevel(DpermitConsts.COOPERATION_LEVEL_PLAT);
        return simple;
    }


}
